import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/entity/order/checkout/order_checkout_entity.dart';
import 'package:distribution_mall_flutter/entity/order/order.dart';
import 'package:distribution_mall_flutter/entity/order/order_trce_entity.dart';
import 'package:distribution_mall_flutter/http/init.dart';
import 'package:distribution_mall_flutter/http/modules/order.dart';
import 'package:distribution_mall_flutter/utils/common_third.dart';
import 'package:distribution_mall_flutter/utils/debug.dart';
import 'package:distribution_mall_flutter/utils/log.dart';
import 'package:distribution_mall_flutter/utils/storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_sign_in/google_sign_in.dart';

class OrderState {
  int? tabIndex;
  String? account;
  String? pad;
  String? code;
  List<OrderTraceEntity> trceList;
  Map<String, dynamic> checkParams;
  late PageController controller = PageController();
  bool enableBtn;
  OrderCreateEntity? createData;

  // List<SaleReasonEntity>

  OrderState(
      {this.tabIndex,
      this.account,
      this.pad,
      this.code,
      this.trceList = const [],
      this.checkParams = const {},
      this.enableBtn = false,
      this.createData});

  OrderState.initial()
      : tabIndex = 0,
        account = '',
        pad = '',
        trceList = const [],
        enableBtn = false,
        checkParams = const {},
        code = '';

  OrderState copyWith({
    int? tabIndex,
    String? account,
    String? pad,
    String? code,
    List<OrderTraceEntity>? trceList,
    OrderCreateEntity? createData,
    Map<String, dynamic>? checkParams,
    bool? enableBtn,
  }) =>
      OrderState(
        tabIndex: tabIndex ?? this.tabIndex,
        account: account ?? this.account,
        pad: pad ?? this.pad,
        code: code ?? this.code,
        trceList: trceList ?? this.trceList,
        createData: createData ?? this.createData,
        checkParams: checkParams ?? this.checkParams,
        enableBtn: enableBtn ?? this.enableBtn,
      );

  get hasOrderTrace {
    return trceList.isNotEmpty;
  }
}

final orderProvider =
    StateNotifierProvider.autoDispose<OrderViewModel, OrderState>((ref) {
  return OrderViewModel();
});

class OrderViewModel extends StateNotifier<OrderState> {
  OrderViewModel() : super(OrderState.initial());

  void changeTabIndex(int index) {
    if (index == state.tabIndex) return;
    state.controller.animateToPage(index,
        duration: const Duration(milliseconds: 400), curve: Curves.easeInOut);
    state = state.copyWith(tabIndex: index);
  }

  void getOrderTrce() async {
    if (!EPstorage.isLogin()) return;
    try {
      List<OrderTraceEntity>? data = await OrderApi.queryInProgressOrderTrace();
      if (data == null) return;
      state = state.copyWith(trceList: data);
    } catch (e) {}
  }

  void resetCheckParams(Map<String, dynamic> map) {
    state = state.copyWith(checkParams: map);
  }

  Future<Result<OrderCreateEntity>> orderCreate(
      Map<String, dynamic> map) async {
    Result<OrderCreateEntity>? data = await OrderApi.orderCreate(map);
    state = state.copyWith(createData: data.data);
    getOrderTrce();
    return data;
  }

  void setEnableBtn(bool bol) {
    state = state.copyWith(enableBtn: bol);
  }
}
